package cn.afactory.tygz.ui.fragments;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.Gendar;
import cn.afactory.tygz.model.SnsSubjectDetailList;
import cn.afactory.tygz.model.SnsUserInfo;
import cn.afactory.tygz.model.SnsUserInfoList;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.adapter.BlackListAdapter;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshBase;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.ui.sns.adapter.UserListAdapter;
import cn.afactory.tygz.ui.sns.controller.SnsControler;

public class BlackListFragment extends BaseFragment implements BlackListAdapter.OnItemClickListener {
    private static final String TAG = BlackListFragment.class.getSimpleName();
    protected SnsControler mController;
    protected BlackListAdapter mAdapter;

    private LayoutInflater minflater;
    protected int mPageIndex = 1;
    protected int mPageSize = 10;
    private long mTargetUserId = -1;
    //用户列表
    protected List<SnsUserInfo> mList = new ArrayList<SnsUserInfo>();

    public BlackListFragment(long targetUserId) {
        mTargetUserId = targetUserId;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mController = new SnsControler(mContext, mUIHandler, null);
    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        this.minflater = inflater;
        View mlayout = inflater.inflate(R.layout.fragment_health_subject, null);
        initViews(mlayout);
        showTitleBar(false);
        return mlayout;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SnsControler.MSG_GET_BLACK_LIST_OK:
                if (msg.obj instanceof SnsUserInfoList) {
                    hideErrorPage(-1);
                    SnsUserInfoList t = (SnsUserInfoList) msg.obj;
                    if (t.value != null && t.value.size() >= 0) {
                        try {
                            mList.addAll(t.value);
                            //是否显示置顶链接VIEW
                            mAdapter.setData(mList);
                            mPageIndex = (mList.size()/mPageSize) + 1;
                            setListListener();

                            if (plistview.isRefreshing()) {
                                plistview.onRefreshComplete();
                            }
                            if (t.value.size() < mPageSize) {
                                plistview.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                            } else {
                                plistview.setMode(PullToRefreshBase.Mode.BOTH);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        if (mAdapter.getCount() == 0) {
                            showNullPage(true);
                        }
                    }
                }
                break;
            case SnsControler.MSG_GET_BLACK_LIST_KO:
                if (mAdapter.getCount() == 0) {
                    hideErrorPage(-1);
                    showErrorPage(msg.arg1, new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            startLoadUserList(true);
                        }
                    });
                } else {
                    LocalUtils.showToast(mContext, (String) msg.obj);
                }
                break;
        }

        // 结束下拉刷新
        if (plistview.isRefreshing()) {
            plistview.onRefreshComplete();
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        startLoadUserList(true);
    }

    /**
     * 初始化组件 views
     *
     * @param view
     */
    private void initViews(View view) {
        plistview = (PullToRefreshListView) view.findViewById(R.id.plv_subject);
        initListView();
    }

    /**
     * 加载listview数据
     */
    protected void startLoadUserList(boolean cleanData) {
        if (cleanData) {
            mPageIndex = 1;
            mList.clear();
        }

        mController.doGetBlackList(mTargetUserId, mPageIndex, mPageSize);
    }

    /**
     * 初始化list view
     */
    private void initListView() {
        mAdapter = new BlackListAdapter(mContext,null);
        plistview.setAdapter(mAdapter);
        plistview.setMode(PullToRefreshBase.Mode.BOTH);
        setListListener();
        plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadUserList(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadUserList(false);
            }
        });
    }

    protected void setListListener() {
        plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadUserList(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                startLoadUserList(false);
            }
        });
        mAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void OnRemoveBlack(TextView v, SnsUserInfo snsUserInfo) {
        showLoadingView("");
        NetManager.getInstance(getActivity()).doBlackUser(snsUserInfo.userId,1, new OnResponseListener<Boolean>() {
            @Override
            public void onInternError(int errorCode, String errorMessage) {
                hideLoadingView();
                LocalUtils.showToast(getActivity(),errorMessage);
            }

            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                hideLoadingView();
                if(isOK && result){
                    startLoadUserList(true);
                }else{
                    LocalUtils.showToast(getActivity(),errorMsg);
                }
            }
        });
    }
}
